<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\shiyao;

use app\api\controller\Controller;
use app\api\model\shiyao\ProjectCenter as CenterModel;
use app\api\model\shiyao\ProjectPay as ProjectPayModel;
use app\api\model\shiyao\ProjectTestee as ProjectTesteeModel;
use app\api\service\User as UserService;
use cores\exception\BaseException;
use think\facade\Db;
use think\response\Json;

/**
 * 管理
 * Class
 * @package app\api\controller
 */
class Center extends Controller {

	public function listAll(): Json {
		// 获取中心列表-全部，用于搜索

		$params = $this->request->param();
		$user = UserService::getCurrentLoginUser();

		$companyId = $user['company_id'];

		$list = Db::name('shiyao_center')
			->where('center_name', 'like', '%' . $params['search'] . '%')
			->where('company_id', 'in', [0, $companyId])
			->select()->toArray();

		return $this->renderSuccess(compact('list'));
	}

	//某中心pi

	public function listPi(): Json {
		// 获取中心列表-全部，用于搜索

		$params = $this->request->param();
		$user = UserService::getCurrentLoginUser();

		$companyId = $user['company_id'];

		$list = Db::name('shiyao_project_center_pi')->alias('pi')
			->join('user', 'user.user_id=pi.user_id')
			->where('pi.center_id', $params['centerId'])
			->field('pi.name,user.idcard_id1,pi.user_id')
			->select()->toArray();

		return $this->renderSuccess(compact('list'));
	}
	//某中心pm-支付申请，收款角色pm

	public function listPm(): Json {
		// 获取中心列表-全部，用于搜索

		$params = $this->request->param();
		$user = UserService::getCurrentLoginUser();

		$companyId = $user['company_id'];

		$list = Db::name('shiyao_project_center_pm')->alias('pm')
			->join('user', 'user.user_id=pm.pm_id')
			->where('pm.center_id', $params['centerId'])
			->field('user.nick_name as name,user.idcard_id1,user.user_id')
			->select()->toArray();

		return $this->renderSuccess(compact('list'));
	}
/**
 * 菜单列表-crc
 * @return Json
 * @throws BaseException
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\DbException
 * @throws \think\db\exception\ModelNotFoundException
 */
	public function getTabsList($centerId): Json {
		$count = Db::name('shiyao_project_pay_log')->where('center_id', $centerId)->where('status', 10)->count(); //待审批
		$count_complate = Db::name('shiyao_project_pay_log')
			->where('center_id', $centerId)
			->where('pay_status', 0)
			->where('status', 11)->count(); //审批完成,未申请支付

		$list = [
			['name' => '全部', 'status' => -1],
			['name' => '待审批', 'status' => 10, 'count' => $count, 'offset' => [5, 10]],
			['name' => '审批完成', 'status' => 11, 'count' => $count_complate, 'offset' => [5, 0]],
			['name' => '审批拒绝', 'status' => 12],
			['name' => '已付款', 'status' => 21],

		];

		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 菜单列表
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function getTabsListPi($centerId): Json {
		$count = Db::name('shiyao_project_pay_log')
			->where('center_id', $centerId)->where('status', 10)->where('pi_status', 0)->count(); //待审批
		//$count_complate = Db::name('shiyao_project_pay_log')->where('pi_status', 1)->count(); //审批完成

		$list = [
			['name' => '全部', 'status' => -1],
			['name' => '待审批', 'status' => 0, 'count' => $count, 'offset' => [5, 35]],
			['name' => '审批完成', 'status' => 1],
			['name' => '审批拒绝', 'status' => 2],

		];

		return $this->renderSuccess(compact('list'));
	}
	public function getTabsListPm($centerId): Json {
		$user = UserService::getCurrentLoginUser();
		$store_user = Db::name('store_user')->where('user_name', $user['mobile'])->find();
		$userId=$user['user_id'];

		//根据角色筛选
		$role = Db::name('store_user_role')->where('role_id', 'in', [10006, 10009])
			->where('store_user_id', $store_user['store_user_id'])->find();

		if ($role) {
			$filter = [];
		} else {
			//审批人员
			$centerIds = Db::name('shiyao_project_center_pm')
				->where('pm_id', $userId)
				->where('center_id', $centerId)
				->group('center_id')->select()->toArray();
			$centerIds = array_column($centerIds, 'center_id');

			//自己添加的项目
			$pmcenter = Db::name('shiyao_project_center')->where('id', $centerId)->where('pm_id', $userId)->field('id')->select()->toArray();
			$centerIds2 = array_column($pmcenter, 'id');
			$newcenterIds = array_merge($centerIds, $centerIds2);

			if ($newcenterIds) {

				$filter[] = ['center_id', 'in', $newcenterIds];
			} else {
				$filter[] = ['center_id', '=', -1];
			}

		}

		$count = Db::name('shiyao_project_pay_log')
			->where('center_id', $centerId)
			->where('status', 10)
			->where('pi_status', 1)
			->where($filter)
			->where('pm_status', 0)->count(); //待审批

		$list = [
			['name' => '全部', 'status' => -1],
			['name' => '待审批', 'status' => 0, 'count' => $count, 'offset' => [5, 35]],
			['name' => '审批完成', 'status' => 1],
			['name' => '审批拒绝', 'status' => 2],
			['name' => '已付款', 'status' => 3],

		];

		return $this->renderSuccess(compact('list'));
	}
	public function getTabsListUser(): Json {

		$list = [
			['name' => '全部', 'status' => -1],
			['name' => '待审批', 'status' => 10],
			['name' => '审批通过', 'status' => 11],

		];

		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 补助列表Pi
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function buzhuListPi(): Json {
		// 获取列表
		$model = new ProjectPayModel;
		$list = $model->getListPi($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	public function buzhuListPm(): Json {
		// 获取列表
		$model = new ProjectPayModel;
		$list = $model->getListPm($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	//获取用户数据
	public function buzhuListUser(): Json {
		// 获取列表
		$model = new ProjectPayModel;
		$list = $model->getListUser($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
/**
 * 补助列表-crc
 * @return Json
 * @throws BaseException
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\DbException
 * @throws \think\db\exception\ModelNotFoundException
 */
	public function buzhuList(): Json {
		// 获取列表
		$model = new ProjectPayModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	/**
	 * 受试者列表
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function peopleList(): Json {
		// 获取列表
		$model = new ProjectTesteeModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 列表
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function list(): Json {
		// 获取列表
		$model = new CenterModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 补助详情
	 * @param int $CenterId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function payDetail(int $logId): Json {
		$detail = ProjectPayModel::detail($logId);

		return $this->renderSuccess(compact('detail'));
	}

	/**
	 * 详情
	 * @param int $CenterId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function detail(int $centerId): Json {
		$detail = CenterModel::detail($centerId);

		$userId = UserService::getCurrentLoginUserId();

		return $this->renderSuccess(compact('detail', 'userId'));
	}
	public function detail_share(int $centerId): Json {
		$detail = Db::name('shiyao_project_center')->alias('c')
			->join('shiyao_project p', 'p.project_id=c.project_id')->where('c.id', $centerId)->field('p.project_name,p.project_code,c.center_code,c.center_name,c.id,p.project_id')->find();

		return $this->renderSuccess(compact('detail'));
	}

	/**
	 * 添加补助记录
	 * @return Json
	 * @throws BaseException
	 */
	public function addBuzhu(): Json {
		$model = new ProjectPayModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess([], '添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 * PI审核
	 * @return Json
	 * @throws BaseException
	 */
	public function editPi(): Json {
		$logId = $this->postForm()['id'];
		$model = ProjectPayModel::detail($logId);
		if ($model['status'] == 30) {
			return $this->renderError('已撤销');
		}
		if ($model->editPi($this->postForm())) {
			return $this->renderSuccess([], '审核成功');
		}
		return $this->renderError($model->getError() ?: '审核失败');
	}

	/**
	 * PM审核
	 * @return Json
	 * @throws BaseException
	 */
	public function editPm(): Json {
		$logId = $this->postForm()['id'];
		$model = ProjectPayModel::detail($logId);

		if ($model->editPm($this->postForm())) {
			return $this->renderSuccess([], '审核成功');
		}
		return $this->renderError($model->getError() ?: '审核失败');
	}

	/**
	 * 编辑补助记录
	 * @return Json
	 * @throws BaseException
	 */
	public function editBuzhu(): Json {
		$logId = $this->postForm()['id'];
		$model = ProjectPayModel::detail($logId);
		if ($model['status'] > 10) {
			return $this->renderError('已审核，不可修改');
		}
		if ($model->edit($this->postForm())) {
			return $this->renderSuccess([], '添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 * 添加
	 * @return Json
	 * @throws BaseException
	 */
	public function add(): Json {
		$model = new CenterModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess([], '添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 *
	 * @param int $CenterId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function edit(): Json {
		$centerId = $this->postForm()['id'];

		$model = CenterModel::detail($centerId);
		if ($model->edit($this->postForm())) {
			return $this->renderSuccess([], '更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	public function state($centerId): Json {
		//$centerId = $this->postForm()['centerId'];

		$model = CenterModel::detail($centerId);
		if ($model->state($this->postForm())) {
			return $this->renderSuccess([], '更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	/**
	 * 删除收货地址
	 * @param int $CenterId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function remove(int $centerId): Json {
		$model = CenterModel::detail($centerId);
		if ($model->remove()) {
			return $this->renderSuccess([], '关闭成功');
		}
		return $this->renderError($model->getError() ?: '关闭成功');
	}
}
